#include "rc2msg.h"
#include <QMap>
#include "gncdbconstant.h"
#include <QtCore/QString>

QString Rc2Msg::getErrorMsg(int rc) {
    static const QMap<int, QString> errorMap = {
        {GNCDB_SUCCESS, "操作成功"},
        {GNCDB_MEM, "内存分配失败"},
        {GNCDB_FILEISNULL, "文件为空"},
        {GNCDB_PARAMNULL, "输入参数为空"},
        {GNCDB_SPACE_LACK, "空间不足"},
        {GNCDB_NOT_FOUND, "未找到"},
        {GNCDB_TABLE_NOT_FOUND, "表未找到"},
        {GNCDB_NO_PAGE, "没有这个页"},
        {GNCDB_FLUSH_FAILED, "刷新失败"},
        {GNCDB_READ_FAILED, "读取失败"},
        {GNCDB_NOT_OPEN, "不能打开"},
        {GNCDB_NO_VALID, "数据库不存在"},
        {GNCDB_CREATE_FALSE, "数据库创建失败"},
        {GNCDB_PARAM_INVALID, "输入参数不合法"},
        {GNCDB_TABLE_EXIST, "表已存在"},
        {GNCDB_DUPLICATE_PRIMARY_KEY, "主键值重复"},
        {GNCDB_PRIMARY_KEY_IMMUTABLE, "主键值不可修改"},
        {GNCDB_CAPOVERFLOW, "容量溢出"},
        {GNCDB_NO_REMOVE, "不能删除"},
        {GNCDB_COLUMN_NOT_FOUND, "列找不到"},
        {GNCDB_ARRAY_ADD_FALSE, "数组插入失败"},
        {GNCDB_PRIMARYKEYINDEXMAP_CREATE_FALSE, "主键列号映射创建失败"},
        {GNCDB_PRIMARYKEYTYPEMAP_CREATE_FALSE, "主键类型映射创建失败"},
        {GNCDB_TABLESCHEMAMAP_CREATE_FALSE, "表模式映射创建失败"},
        {GNCDB_MASTER_TS_CREATE_FALSE, "主表模式创建失败"},
        {GNCDB_SCHEMA_TS_CREATE_FALSE, "模式表模式创建失败"},
        {GNCDB_MASTERTABLE_CREATE_FALSE, "主表构造失败"},
        {GNCDB_SCHEMATABLE_CREATE_FALSE, "模式表构造失败"},
        {GNCDB_TABLEMAP_CREATE_FALSE, "表映射构造失败"},
        {GNCDB_BTC_CREATE_FALSE, "B树游标构造失败"},
        {GNCDB_ARRAY_CREATE_FALSE, "数组创建失败"},
        {GNCDB_MASTERFIELDARRAY_NOT_FOUND, "主表字段数组找不到"},
        {GNCDB_ARRAY_GETPOINTER_FALSE, "数组查找失败"},
        {GNCDB_CONDITION_CREATE_FALSE, "条件构造失败"},
        {GNCDB_SCHEMATUPLE_NOT_FOUND, "模式元组没有找到"},
        {GNCDB_COLUMNCONSTRAINT_CREATE_FALSE, "列约束构造失败"},
        {GNCDB_COLUMN_CREATE_FALSE, "列构造失败"},
        {GNCDB_TABLESCHEMA_CREATE_FALSE, "表模式构造失败"},
        {GNCDB_BTREETABLE_CREATE_FALSE, "B树表构造失败"},
        {GNCDB_BTREEDFS_FALSE, "DFS失败"},
        {GNCDB_LEAFTUPLE_CREATE_FALSE, "元组构造失败"},
        {GNCDB_INTFIELD_CREATE_FALSE, "整数字段构造失败"},
        {GNCDB_LEAFTUPLE_ADD_FALSE, "元组添加字段失败"},
        {GNCDB_VARCHARFIELD_CREATE_FALSE, "变长字符串字段构造失败"},
        {GNCDB_INSERTTTUPLE_FALSE, "B树表插入元组失败"},
        {GNCDB_REALFIELD_FALSE, "实数字段构造失败"},
        {GNCDB_MASTERTUPLE_NOT_FOUND, "主表元组找不到"},
        {GNCDB_FIELD_NOT_EXIST, "属性不存在"},
        {GNCDB_MAP_CREATE_FALSE, "映射创建失败"},
        {GNCDB_STATUS_NOT_FOUND, "页面状态找不到"},
        {GNCDB_MAP_GET_FALSE, "映射查找失败"},
        {GNCDB_ARRAY_GET_FALSE, "数组获取失败"},
        {GNCDB_BTG_CREATE_FALSE, "B树页面构造失败"},
        {GNCDB_FREEPAGE_CREATE_FALSE, "空闲页面构造失败"},
        {GNCDB_OVERFLOWPAGE_CREATE_FALSE, "溢出页面构造失败"},
        {GNCDB_ARRAY_REMOVE_FALSE, "数组删除失败"},
        {GNCDB_MAP_PUT_FALSE, "映射插入失败"},
        {GNCDB_MAP_REMOVE_FALSE, "映射删除失败"},
        {GNCDB_STATUS_CREATE_FALSE, "页面状态构造失败"},
        {GNCDB_TOBYTE_FALSE, "字节流转换失败"},
        {GNCDB_MAP_ITERATOR_FALSE, "映射迭代器创建失败"},
        {GNCDB_MAP_NEXT_NOT_FOUND, "映射下一个找不到"},
        {GNCDB_PAGETYPE_NOT_FOUND, "页面类型错误"},
        {GNCDB_TABLE_PARAM_FALSE, "表只能读，不能写"},
        {GNCDB_DEEPCOPY_FAIL, "深拷贝失败"},
        {GNCDB_LEAFPAGE_NOT_FOUND, "获取叶子页面失败"},
        {GNCDB_NOT_FOUND_PAGE, "找不到指定的页面"},
        {GNCDB_PRIMARYKEY_NOTNULL, "主键值不能为空"},
        {GNCDB_WAS_CLOSED, "数据库已经关闭"},
        {GNCDB_AIIPAGE_IS_PIN, "所有的页面均被钉住"},
        {GNCDB_LOCK_FAIL, "申请锁失败"},
        {GNCDB_UN_JOIN, "元组不满足条件无法连接"},
        {GNCDB_ROWS_OVERFLOW, "表已经达到最大行数"},
        {GNCDB_LOG_READ_ERROR, "日志读取失败"},
        {GNCDB_FILE_NOT_FOUND, "数据库文件不存在"},
        {GNCDB_BLOB_EXIST, "大对象文件已存在"},
        {GNCDB_BLOB_NO_DELETE, "元组中存在大对象无法删除"},
        {GNCDB_SYSTEMTABLE_NOTREMOVE, "系统表不能删除"},
        {GNCDB_NOT_REFACTOR, "主表和模式表禁止插入"},
        {GNCDB_TABLESCHEMA_NOT_FOUND, "表模式找不到"},
        {GNCDB_WRITE_FILE_FAILED, "写文件失败"},
        {GNCDB_OLD_PAGE_EXIST, "页面已备份"},
        {GNCDB_BLOB_NOT_EXIST, "元组无大对象"},
        {GNCDB_PARSE_FALSE, "解析失败"},
        {GNCDB_INTERNAL, "内部错误"},
        {GNCDB_SQL_SYNTAX, "SQL语法错误"},
        {GNCDB_UNIMPLENMENT, "暂未实现"},
        {GNCDB_SCHEMA_NUM_MISMATCH, "表模式数量不匹配"},
        {GNCDB_SCHEMA_FIELD_TYPE_MISMATCH, "表模式类型不匹配"},
        {GNCDB_SCHEMA_FIELD_MISSING, "无效字段名"},
        {GNCDB_NEXT_EOF, "遍历结束"},
        {GNCDB_INSERT_FAILED, "插入失败"},
        {GNCDB_TUPLE_NOT_FOUND, "元组未找到"},
        {GNCDB_EXPR_EVAL_FAILED, "表达式求值失败"},
        {GNCDB_JOIN_KEY_NOT_FOUND, "连接键未找到"},
        {GNCDB_DATE_TYPE_INVALID, "日期类型无效"},
        {GNCDB_ORDER_BY_INVALID, "排序无效"},
        {GNCDB_DATETIME_TYPE_INVALID, "日期时间类型无效"},
        {GNCDB_FIELD_NOT_ALLOW_NULL, "字段不允许为空"},
        {GNCDB_TEXT_LENGTH_EXCEED, "文本长度超出限制"},
        {GNCDB_ROW_DONE, "行处理结束"},
        {GNCDB_GET_TABLE_ROWNUM_FAILED, "获取表行数失败"},
        {GNCDB_VARARRAYLIST_NULL, "变长数组列表为空"},
        {GNCDB_TYPE_CAST_INVALID, "类型转换无效"},
        {GNCDB_PREDICATE_PUSHDOWN_FAILED, "谓词下推失败"},
        {GNCDB_SORT_MERGE_NO_CONDITION, "排序合并连接没有连接条件"},
        {GNCDB_CONDITION_INVALID, "条件无效"},
        {GNCDB_SQLFILE_INVALID, "SQL文件无效"},
        {GNCDB_JOINLIST_NULL, "连接列表为空"},
        {GNCDB_PARENTPAGEID_NOT_EXIST, "父页面ID不存在"}
    };

    return errorMap.value(rc, "未知错误");
}

QString Rc2Msg::getErrorMsg(int rc, const QString& customMsg) {
    QString baseMsg = getErrorMsg(rc);
    if (!customMsg.isEmpty()) {
        return QString("%1: %2").arg(baseMsg).arg(customMsg);
    }
    return baseMsg;
} 